package com.shcz.migu.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by Sea Zhang on 2017/3/9.
 */

public class DateUtils {

    private static String mYear; // 当前年
    private static String mMonth; // 月
    private static String mDay;
    private static String mWay;
    public static String NETDATEFORMATE="yyyy-MM-dd hh:mm:ss";
    public static String UIDATEFORMATE="yyyy年MM月dd日 hh:mm:ss";
    public static String UIDATEFORMATE2="yyyy年MM月dd日";


    /**
     * 获取当前日期几月几号
     */
    public static String getDateString() {


        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if(Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(Integer.parseInt(mMonth)))){
            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(Integer.parseInt(mMonth))));
        }
        return mMonth + "月" + mDay + "日";
    }

    public static Date strToDateLong(String strDate, String formate) {
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        Date strtodate = null;
        try {
            strtodate = formatter.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strtodate;
    }
    /**
     * 时间格式转换
     */
    public static String getFormateDate(String dateStr, String oldFromate, String formate) {
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        if(TextUtils.isEmpty(dateStr))
            return "";
        dateStr=dateStr.replaceAll("T"," ");
        dateStr=dateStr.substring(0,dateStr.length()-4);
        String dateString = formatter.format(strToDateLong(dateStr,oldFromate));
        return dateString;
    }
    public static String getFormateDateBirth(String dateStr, String oldFromate, String formate) {
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        if(TextUtils.isEmpty(dateStr))
            return "";

        String dateString = formatter.format(strToDateLong(dateStr,oldFromate));
        return dateString;
    }

    public static String getCompareIntHour(String dateStr, String newDateStr, String formate) {


        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        Date oldDate=null;
        Date newDate=null;
        try {
            oldDate=formatter.parse(dateStr);
            newDate =formatter.parse(newDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long newLong=newDate.getTime();
        long oldLong=oldDate.getTime();
        int hour= (int) ((newLong-oldLong)/1000/60/60/60);

        return hour+"";
    }

    public static String getCompareIntDay(String dateStr, String newDateStr, String formate) {
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        Date oldDate=null;
        Date newDate=null;
        try {
            oldDate=formatter.parse(dateStr);
            newDate =formatter.parse(newDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        newDate.compareTo(oldDate);
        long newLong=newDate.getTime();
        long oldLong=oldDate.getTime();
//        long gmMils=DateUtil.getGMMils();
        int hour= (int) ((newLong-oldLong)/1000/60/60);
        if (hour == 0)
        {
            int mils = (int) ((newLong - oldLong) / 1000);
            if (mils > 60) {
                return mils / 60 + "分前";
            } else {
                return mils + "秒前";
            }
        } else {
            if (hour / 24 > 0) {
                return hour / 24 + "天前";
            } else
                return hour + "小时前";
        }



    }
    public static String getBeforeDay(String dateStr, String oldformat, String newFormate){

        String date= DateUtil.getConvertDateString(dateStr,oldformat
                ,newFormate);
        date=DateUtil.formateGrenLocalData(date,newFormate,newFormate);
        return DateUtils.getCompareIntDay(date, DateUtils.getCurrentTime(DateUtil.DATE_FORMAT_10),DateUtil.DATE_FORMAT_10);
    }


    /**
     * 获取当前年月日
     *
     * @return
     */
    public static String StringData() {


        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if(Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(Integer.parseInt(mMonth)))){
            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(Integer.parseInt(mMonth))));
        }
        return mYear + "-" + (mMonth.length()==1?"0"+mMonth:mMonth) + "-" + (mDay.length()==1?"0"+mDay:mDay);
    }


    /**
     * 根据当前日期获得是星期几
     *
     * @return
     */
    public static String getWeek(String time) {
        String Week = "周";


        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        try {


            c.setTime(format.parse(time));


        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "日";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }
        return Week;
    }


    /**
     * 获取今天往后一周的日期（几月几号）
     */
    public static List<String> getSevendate() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));


        for (int i = 0; i < 7; i++) {
            mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
            if(Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(i+1))){
                mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),(i+1)));
            }
            String date = mMonth + "月" + mDay + "日";
            dates.add(date);
        }
        return dates;
    }


    /**
     * 获取今天往后一周的集合
     */
    public static List<String> get7week() {
        String week = "";
        List<String> weeksList = new ArrayList<String>();
        List<String> dateList = get7date();
        for (String s : dateList) {
            if (s.equals(StringData())) {
                week = "今天";
            } else {
                week = getWeek(s);
            }
            weeksList.add(week);
        }
        return weeksList;
    }


    public static List<String> get7date() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        SimpleDateFormat sim = new SimpleDateFormat(
                "yyyyMMdd");
        String date = sim.format(c.getTime());
        dates.add(date);
        for (int i = 0; i < 6; i++) {
            c.add(Calendar.DAY_OF_MONTH, 1);
            date = sim.format(c.getTime());
            dates.add(date);
        }
        return dates;
    }

    /**得到当年当月的最大日期**/
    public static int MaxDayFromDay_OF_MONTH(int year,int month){
        Calendar time= Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR,year);
        time.set(Calendar.MONTH,month-1);//注意,Calendar对象默认一月为0
        int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }

    public static String getCurrentTime(String timeFormate) {
        long newestRequestTime = System.currentTimeMillis();//"yyyy-MM-dd HH:mm:ss"
        String time = getDateStr(newestRequestTime, timeFormate);
        return time;
    }
    public static String getDateStr(long time, String format) {
        if (time == 0)
            return null;
        Date date = new Date(time);
        return getDateStr(date, format);
    }
    public static String getDateStr(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String dateStr = dateFormat.format(date);
        return dateStr;
    }
}
